﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ManagementConsole;
using System.Timers;

namespace SharpShare.Management.Mmc.Views {
    public class OpenFilesListView : TypeDescriptorReportListView<Data.OpenFileInfo>, IEqualityComparer<Data.OpenFileInfo> {
        protected override bool ShouldRefreshPeriodically {
            get {
                return true;
            }
        }
        protected override IEnumerable<Data.OpenFileInfo> ListReportObjects() {
            try {
                var files = ((SharpShareSnapIn)this.SnapIn).ShareManager.Sessions
                    .SelectMany(s => s.OpenFiles);

                return files.ToList();
            } catch { }

            return Enumerable.Empty<Data.OpenFileInfo>();
        }
        protected override ResultNode CreateResultNode(Data.OpenFileInfo reportObject) {
            ResultNode node =  base.CreateResultNode(reportObject);
            node.ImageIndex = (int)Icon.Files;
            return node;
        }

        protected override IEqualityComparer<Data.OpenFileInfo> ReportObjectComparer {
            get {
                return this;
            }
        }

        #region IEqualityComparer<OpenFileInfo> Members

        bool IEqualityComparer<Data.OpenFileInfo>.Equals(Data.OpenFileInfo x, Data.OpenFileInfo y) {
            if (x == null && y == null)
                return false;
            if (x == null || y == null)
                return false;
            return (x.Identifier.Equals(y.Identifier));
        }

        int IEqualityComparer<Data.OpenFileInfo>.GetHashCode(Data.OpenFileInfo obj) {
            return obj.Identifier.GetHashCode();
        }

        #endregion
    }
}
